package com.walk.zworld.ui.adapter

import android.app.ActivityOptions
import android.view.LayoutInflater
import android.widget.TextView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.google.android.flexbox.FlexboxLayout
import com.walk.zworld.R
import com.walk.zworld.webView.WebViewActivity
import com.base.lib.base.adapter.BaseSkeletonAdapter
import com.base.lib.httpUtils.Article
import com.base.lib.httpUtils.NavigationBean
import java.util.*

class SysNavigationAdapter : BaseSkeletonAdapter<NavigationBean, BaseViewHolder>(R.layout.item_knowledge_tree_list) {

    private var mInflater: LayoutInflater? = null
    private val mFlexItemTextViewCaches: Queue<TextView> = LinkedList()
    private var mOnItemClickListener: OnItemClickListener? = null

    open fun setOnItemClickListener(onItemClickListener: OnItemClickListener) {
        mOnItemClickListener = onItemClickListener
    }

    override fun convert(holder: BaseViewHolder, item: NavigationBean) {
        holder.setText(R.id.tv_knowledge_item_title, item.name)
        val fbl: FlexboxLayout = holder.getView(R.id.fbl_knowledge)
        item.articles.let {
            for (i in 0 until it.size) {
                val childTextView = createOrGetCacheFlexItemTextView(fbl)
                childTextView.text = it[i].title
                childTextView.setOnClickListener {
                    when {
                        mOnItemClickListener != null -> mOnItemClickListener?.onClick(
                            item.articles[i],
                            i
                        )
                        else -> {
                            val options: ActivityOptions = ActivityOptions.makeScaleUpAnimation(
                                it,
                                it.width / 2,
                                it.height / 2,
                                0,
                                0
                            )
                            val data: Article = item.articles[i]
                            WebViewActivity.start(
                                context,
                                data.id,
                                data.title,
                                data.link,
                                options.toBundle()
                            )

                        }
                    }
                }
                fbl.addView(childTextView)
            }
        }
    }

    private fun createOrGetCacheFlexItemTextView(fbl: FlexboxLayout): TextView {
        val tv = mFlexItemTextViewCaches.poll()
        return tv ?: createFlexItemTextView(fbl)
    }

    private fun createFlexItemTextView(fbl: FlexboxLayout): TextView {
        if (mInflater == null) {
            mInflater = LayoutInflater.from(fbl.context)
        }
        return mInflater!!.inflate(R.layout.item_knowledge_text, fbl, false) as TextView
    }

    override fun onViewRecycled(holder: BaseViewHolder) {
        super.onViewRecycled(holder)
        val fbl = holder.getView<FlexboxLayout>(R.id.fbl_knowledge)
        for (i in 0 until fbl.childCount) {
            mFlexItemTextViewCaches.offer(fbl.getChildAt(i) as TextView)
        }
        fbl.removeAllViews()
    }

    interface OnItemClickListener {
        fun onClick(bean: Article, pos: Int)
    }
}